package com.xisoft.common.thread;

import lombok.SneakyThrows;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * @fileRelativePath: HandleCallable.java
 * @Date: Created on 2021/9/16  14:27  <br>
 * @author: LiBencheng<br>
 * @version: 1.0<br>
 */
public class HandleCallable< E > implements Callable< ThreadResultBean >
{
    /** 线程名称 */
    private String threadName = "";
    /** 需要处理的数据 */
    private List< E > data;
    /** 辅助参数 */
    private Map< String,Object > params;
    /** 具体执行任务 */
    private IThreadTask< ThreadResultBean< Object >,E > task;

    public HandleCallable( )
    { }

    public HandleCallable(String threadName, List< E > data, Map< String,Object > params,
            IThreadTask< ThreadResultBean< Object >,E > task)
    {
        this.threadName = threadName;
        this.data = data;
        this.params = params;
        this.task = task;
    }

    @SneakyThrows @Override
    public ThreadResultBean< List< ThreadResultBean< Object > > > call( ) throws Exception
    { return exeCall();  }

    /**
     * *********************************************************** <br>
     * 说明： 执行结果
     *
     * @return
     * @throws Exception
     * @ThreadResultBean<List<ThreadResultBean<Object>>>
     * @author licheng
     * @date Created on 2019-8-4
     * @time 下午3:14:43
     * @version 1.0 <br>
     * ************************************************************ <br>
     * @see
     */
    public ThreadResultBean< List< ThreadResultBean< Object > > > exeCall( )
    {
        // 该线程中所有数据处理返回结果
        ThreadResultBean< List< ThreadResultBean< Object > > > resultBean = ThreadResultBean.newInstance();
        // 返回结果集
        List< ThreadResultBean< Object > > resultList = new ArrayList< ThreadResultBean< Object > >();
        // 循环处理每个数据
        for (int i = 0, j = data == null ? 0 : data.size(); i < j; i++)
        {
            // 需要执行的数据
            E e = data.get(i);
            // 将数据执行结果加入到结果集中
            resultList.add(task.execute(e, params));
        }
        resultBean.setData(resultList);
        resultBean.setThreadName(getThreadName());
        return resultBean;
    }

    public String getThreadName( )
    {
        return threadName;
    }

    public void setThreadName(String threadName)
    {
        this.threadName = threadName;
    }

}
